﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Gra.Seguros.Servicio.Be;


namespace Gra.Seguros.Servicio.Dalc
{
    public class RecepcionDocumentoDalc : IDisposable
    {


        #region "Procedimientos Consultas"
        public List<RecepcionDocumentoBe> listar(int _cRecepcionDocumento, int _cAseguradora, int _cDocumento)
        {
            try
            {
                List<RecepcionDocumentoBe> lst = new List<RecepcionDocumentoBe>();
                using (SqlCommand oCommand = new SqlCommand("uspListarRecepcionDocumento", oCn))
                {
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@cRecepcionDocumento", SqlDbType.Int).Value = _cRecepcionDocumento;
                    oCommand.Parameters.Add("@cAseguradora", SqlDbType.Int).Value = _cAseguradora;
                    oCommand.Parameters.Add("@cDocumento", SqlDbType.Int).Value = _cDocumento;
                                        
                    using (SqlDataReader drListado = oCommand.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection))
                    {
                        if (drListado.HasRows)
                        {
                            while ((drListado.Read()))
                            {
                                RecepcionDocumentoBe obj = new RecepcionDocumentoBe();
                                obj.cRecepcionDocumento = drListado.GetInt32(drListado.GetOrdinal("cRecepcionDocumento"));
                                obj.Documento = new DocumentoBe();
                                obj.Documento.cDocumento = drListado.GetInt32(drListado.GetOrdinal("cDocumento"));
                                obj.Documento.dDocumento = drListado.GetString(drListado.GetOrdinal("dDocumento"));
                                obj.fRecepcionDocumento = drListado.GetString(drListado.GetOrdinal("fRecepcionDocumento"));
                                
                                lst.Add(obj);
                            }
                        }
                    }
                }

                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion



        #region "Procedimientos Mantenimientos"

        public string registrarRecepcionDocumento(RecepcionDocumentoBe _objCab)
        {
            string vReturn = string.Empty;
            try
            {
                using (SqlCommand oCommand = new SqlCommand("uspRegistrarRecepcionDocumento", oCn))
                {

                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add(new SqlParameter("@cRecepcionDocumento", SqlDbType.Int)).Value = _objCab.cRecepcionDocumento;
                    oCommand.Parameters.Add(new SqlParameter("@cDocumento", SqlDbType.Int)).Value = _objCab.Documento.cDocumento;
                    oCommand.Parameters.Add(new SqlParameter("@nDocumento", SqlDbType.Int)).Value = _objCab.nDocumento;
                    oCommand.Parameters.Add(new SqlParameter("@cAseguradora", SqlDbType.Int)).Value = _objCab.Aseguradora.cAseguradora;
                    oCommand.Parameters.Add(new SqlParameter("@cCliente", SqlDbType.Int)).Value = _objCab.Cliente.cCliente;
                    oCommand.Parameters.Add(new SqlParameter("@dObservacion", SqlDbType.VarChar, 120)).Value = _objCab.dObservacion;
                    oCommand.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.VarChar, 500)).Direction = ParameterDirection.Output;
                    oCommand.ExecuteNonQuery();
                    vReturn = Convert.ToString(oCommand.Parameters["@RETURN_VALUE"].Value);
                }
                return vReturn;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region "Constructor & Disposable"
            private bool _disposed = false;
            SqlConnection oCn = null;
            public RecepcionDocumentoDalc()
            {
                string strConnection = ConfigurationManager.ConnectionStrings["dbSeguros"].ConnectionString;
                oCn = new SqlConnection(strConnection);
                oCn.Open();
            }

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            protected virtual void Dispose(bool disposing)
            {
                if (!_disposed)
                {
                    if (disposing)
                    {
                        //Dispose managed resources here. llamar componetes de conexion u otros.                        
                        if (oCn.State != ConnectionState.Closed)
                        {
                            oCn.Close();
                        }
                        oCn.Dispose();
                    }
                    //Disposed unmanaged resources here.
                    //set the _disposed flag to prevent subsequent disposals.
                    _disposed = true;
                }
            }
            //Finalization code.
            ~RecepcionDocumentoDalc()
            {
                Dispose(false);
            }
        #endregion
    }
}
